#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    button=new QPushButton(this);
    button->setText("按钮");
    button->move(10,10);
    connect(button,&QPushButton::clicked,this,&Widget::handlerClicked1);
    qDebug()<<"按钮1 conncet success"<<endl;

    button2=new QPushButton(this);
    button2->setText("点击-按钮-更换标题1为标题2");
    button2->move(50,50);
    connect(button2,&QPushButton::clicked,this,&Widget::handlerClicked);

    QPushButton*button3=new QPushButton(this);
    button3->setText("lambda表达式按钮");
    button3->move(200,20);
    //使用lambda表达式
    //[捕获变量](参数){函数体}
    connect(button3,&QPushButton::clicked,this,
            [button3](){
        if(button3->text()==QString("lambda表达式按钮"))
        {
            button3->setText("lambda~~~");
        }
        else if(button3->text()==QString("lambda~~~"))
        {
            button3->setText("lambda表达式按钮");
        }
        else
        {
            qDebug()<<"出现bug"<<endl;
        }
    }
            );
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handlerClicked1()
{
    this->setWindowTitle("标题1");
    qDebug()<<"handlerClicked1()"<<endl;
}

void Widget::handlerClicked2()
{
    this->setWindowTitle("标题2");
    qDebug()<<"handlerClicked2()"<<endl;
}

void Widget::handlerClicked()
{
    disconnect(button,&QPushButton::clicked,this,&Widget::handlerClicked1);
    qDebug()<<"disconnect success"<<endl;

    connect(button,&QPushButton::clicked,this,&Widget::handlerClicked2);
    qDebug()<<"connect success"<<endl;
}



